const db = wx.cloud.database();

Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    option1: [
      { text: '全部商品', value: 0 },
      { text: '新款商品', value: 1 },
      { text: '活动商品', value: 2 }
    ],
    option2: [
      { text: '默认排序', value: 'a' },
      { text: '好评排序', value: 'b' },
      { text: '销量排序', value: 'c' }
    ],
    value1: 0,
    value2: 'a',
    allGoods:[],
    tapDBName:null,
    allCategory:null,
    category:null,
    value:''
  },
 
  onLoad: function (options) {
    wx.hideShareMenu();
    //category热门推荐、欧美大牌、中国大陆
    const category=options.category;
    //数据库
    var categoryDBName=options.tapDatabaseName
    console.log(category);
    //调用前一个页面跳转过来相应的数据库 allCategory01……
    const allCategory = db.collection(categoryDBName);
    console.log(allCategory)
    this.setData({
      allCategory:allCategory,
      category: category
    })
    //正则表达式查询
    
    allCategory.where({
      title: {
        $regex:'.*' + category + '.*',
        $options:'i'
    }
    })
    .get().then(res => {
      console.log(res.data)
      this.setData({
        allGoods: res.data
      })
    }),
    this.setData({
      tapDBName: categoryDBName
    })
  },
  onChange(e) {
    this.setData({
      value: e.detail,
    });
  },
  onSearch() {
    console.log('搜索' + this.data.value);
    var allCategory=this.data.allCategory;
    allCategory.where({
      text: db.RegExp({
        regexp: this.data.value,
        options: 'i',
      })
    }).get().then(res => {
      console.log(res.data)
      this.setData({
        allGoods: res.data
      })
    })
  },
  changeValue1: function (event) {
    console.log(event.detail)
    var allCategory=this.data.allCategory;
    var tagName;
   if(event.detail==1){
      tagName="新款商品"
    }
    else if(event.detail==2){
      tagName = "活动商品"
    }
    if(event.detail==0){
      allCategory.where({
        title: {
          $regex: '.*' + this.data.category + '.*',
          $options: 'i'
        }
      }).get().then(res => {
        //console.log(res.data)
        this.setData({
          allGoods: res.data
        })
      })
    }else {
    allCategory.where({
      tag: tagName,
      title: {
        $regex: '.*' + this.data.category + '.*',
        $options: 'i'
      }
    }).get().then(res => {
      //console.log(res.data)
      this.setData({
        allGoods: res.data
      })
    })
    }
  },
  changeValue2:function(event){
    console.log(event.detail)
    var allCategory = this.data.allCategory;
    var indexName;
    if(event.detail=='b'){
      indexName = "score"
      console.log(indexName)
    }
    else if (event.detail == 'c'){
      indexName = "saleNumber"
    }
    if(event.detail=='a'){
      console.log(a)
      allCategory.where({
        title: {
          $regex: '.*' + this.data.category + '.*',
          $options: 'i'
        }
      }).get().then(res => {
        //console.log(res.data)
        this.setData({
          allGoods: res.data
        })
      })
    }
    else{
      allCategory.where({
        title: {
          $regex: '.*' + this.data.category + '.*',
          $options: 'i'
        }
      }).orderBy(indexName, 'desc').get().then(res => {
        this.setData({
          allGoods: res.data
        })
      })
    }
    
  },

  backPageTap:function(event){
    wx.switchTab({
      url: '../../classify/classify',
      success:function(){
        console.log("jump success")
      }
    })
    
  },
  onTapToDetail:function(event){
    var goodname = event.currentTarget.dataset.goodname;
    var goodsId = event.currentTarget.dataset.id;
    console.log("goodsName" + goodname)
    wx.navigateTo({
      url: '../../detail/detail?goodsId=' + goodsId +'&tapDBName='+this.data.tapDBName+'&goodname='+goodname
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }

  
})